﻿package{
    import flash.display.Sprite;
    import flash.display.Shape;
    import flash.events.Event;
    import flash.events.MouseEvent;
	import flash.ui.Mouse;
    public class Canvas extends Sprite{
        private var bg:Bg;
        private var oldX:Number;
        private var oldY:Number;
        private var _pressed:Boolean = false;
        public static const CANVAS_WIDTH:uint = 760;
        public static const CANVAS_HEIGHT:uint = 480;
        public static const LINE_COLOR:uint = 0xff0000;
        private var _space:Number = 40;
        //功能
        public static const FREE_HAND:uint = 1;
        public static const LINE:uint = 2;
        public static const ERASE:uint = 3;
        private var lineColor:uint;
        private var _statusId:uint;

        private var _boxArr:Array;//box
        private var shapeArr:Array;//所有绘图形状
        private var _redoList:Array; //恢复历史
        private var _customCursor:Sprite;
        private var _cursor:String;
        private var star:Star;//星星图形
        private var eraseMc:CustomErase;//橡皮檫图形
        private var eraser:CustomErase;
        public function Canvas():void{
            addEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
        }

        private function onAddedToStage(e:Event):void{
            Mouse.hide();
            cursor = "star";
            _customCursor = new Star();
            addChild(_customCursor);

            star = new Star();
            eraseMc = new CustomErase();

            _statusId = FREE_HAND;
            _boxArr = new Array();
            shapeArr = new Array();
            _redoList = new Array();

            bg = new Bg();
            addChild(bg);

            removeEventListener(Event.ADDED_TO_STAGE, onAddedToStage);
            addEventListener(MouseEvent.MOUSE_DOWN, onMouseDownHandler);
            addEventListener(MouseEvent.MOUSE_MOVE, onMouseMoveHandler);
        }

        private function render(e:Event):void{
            _customCursor.x = stage.mouseX;
            _customCursor.y = stage.mouseY;

            if(_pressed && _cursor == "eraser"){
                if(shapeArr.length > 0){
                    checkCollision();
                }
            }
        }

        private function onMouseDownHandler(event:MouseEvent):void{
            _pressed = true;
            oldX = stage.mouseX;
            oldY = stage.mouseY;
            
            if(_statusId == FREE_HAND || _statusId == LINE){
                addShapes(0xfffeff, _statusId);
            }else if(_statusId == ERASE){
            }
            addEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
        }

        private function addShapes(lineColor:uint, statusId:uint):void{
            this.lineColor = lineColor;
            this._statusId = statusId;
            var starBox:Star = new Star();
            addChild(starBox);
            _boxArr.push(starBox);

            var star:Star = new Star();
            starBox.addChild(star);
            star.x = stage.mouseX;
            star.y = stage.mouseY;
            shapeArr.push(star);
        }
        

        private function onMouseMoveHandler(event:MouseEvent):void{
            customCursor.x = stage.mouseX;
            customCursor.y = stage.mouseY;
            if(_pressed == true){
                var dx:Number = stage.mouseX - oldX;
                var dy:Number = stage.mouseY - oldY;
                var rad:Number = Math.atan2(dy, dx);
                var dist:Number = dist(dx, dy);
                var newX:Number = _space * Math.cos(rad) + oldX;
                var newY:Number = _space * Math.sin(rad) + oldY;
                var star:Star;
                switch(_statusId){
                    case FREE_HAND:
                        if(dist >= _space){
                            star = new Star();
                            _boxArr[_boxArr.length - 1].addChild(star);
                            shapeArr.push(star);
                            star.x = newX;
                            star.y = newY;
                            oldX = newX;
                            oldY = newY;
                        }
                        break;
                    case LINE: 
                        if(_boxArr.length > 0){
                            trace(_boxArr[_boxArr.length - 1].numChildren);
                            while(_boxArr[_boxArr.length - 1].numChildren > 0){
                                _boxArr[_boxArr.length - 1].removeChildAt(0);
                            }
                        }
                        var num:uint = dist / _space;
                        for(var i:uint = 0; i < num + 1; i++){
                            star = new Star();
                            _boxArr[_boxArr.length - 1].addChild(star);
                            shapeArr.push(star);
                            star.x = oldX + _space * i * Math.cos(rad);
                            star.y = oldY + _space * i * Math.sin(rad);
                        }
                        break;
                }
            }
        }
        private function checkCollision():void{
            var len:uint = shapeArr.length;
            for(var i:uint = 0; i < len; i++){
                var temp:Star = shapeArr[i];
                if(eraseMc.hitTestObject(temp)){
                    trace("------------collision-------------");
                    //shapeArr.splice(i, 1);
                    //temp.parent.removeChild(temp);
                }
            }
        }

        private function onMouseUpHandler(event:MouseEvent):void{
            _pressed = false;
            if(_statusId == ERASE)
                eraseMc.stopDrag();
            removeEventListener(MouseEvent.MOUSE_MOVE, onMouseMoveHandler);
            removeEventListener(MouseEvent.MOUSE_UP, onMouseUpHandler);
        }
        
        private function dist(dx:Number, dy:Number):Number{
            return Math.sqrt(dx * dx + dy * dy);
        }

    /*-----------------getter and setter----------------*/
        public function get statusId():uint{
            return _statusId;
        }
        
        public function set statusId(value:uint):void{
            _statusId = value;
        }

        public function get customCursor():Sprite{
            return _customCursor;
        }
        
        public function set customCursor(value:Sprite):void{
            _customCursor = value;
        }

        public function get cursor():String{
            return _cursor;
        }
        
        public function set cursor(value:String):void{
            _cursor = value;
            if(_cursor == "star"){
                _customCursor = star;
            }else if(_cursor == "eraser"){
                _customCursor = eraseMc;
            }
        }

        public function get boxArr():Array{
            return this._boxArr;
        }
        
        public function set boxArr(value:Array):void{
            this._boxArr = value;
        }
        
        public function get redoList():Array{
            return this._redoList;
        }
        
        public function set redoList(value:Array):void{
            this._redoList = value;
        }
    }
}
